// 下载EXCEL文件
var testinput,downFile;
var app = getApp();////cloud_data = "xiaoqu_jinghunanyuan_getdata", cloud_excex ="xiaoqu_jinghunanyuan";
var dep_list, hx_index = 0, pic_array, zqaddress, database;
var util = require('../../utils/util.js');
var date = util.formatDate(new Date());
Page({
  data: { 
    feliddown: true,
    hx_index: 0,
    date: date,
  },
  onLoad: function (options) {
    this.dep_listf();
    this.findOpenid();
  },
  bindDateChange: function (e) {

    this.setData({
      date: e.detail.value
    })
    date = e.detail.value
   
  },
  down_excel: function (res) { 
    let that = this;    
    
    if (zqaddress != "请选择") {     
                  if (downFile.includes(pic_array[hx_index].value)) { ///测试人员权限 downFile
                    
                    // console.log('选择的PICK:', pic_array[hx_index].value)
                    // console.log('downFile:', downFile) 
                    // console.log('database:', database)
                    // console.log('database:', date)
                          //读取users表数据
                              wx.cloud.callFunction({
                                name: "zmyy_excel_getdata",///////////////传参数
                                data :{                                 
                                  gz_address: pic_array[hx_index].value,  ///读取数据库中工作地点的数据
                                  database: database , ///要读取的数据库,在工作人员表中的字段
                                  gzry_addtime:date
                                },
                                success(res) {
                                  console.log("读取成功cloud_data", res.result.data)
                                  wx.showToast({
                                    title: '上传文件请等待',
                                    icon: 'success',
                                  })
                                  that.savaExcel(res.result.data)
                                },
                                fail(res) {
                                  console.log("读取失败getusers", res)
                                }
                              })
                          
                  }
                  else {
                    wx.showToast({
                      title: '没有权限下载',
                      icon: 'success',
                    })
                    console.log('测试变量false:', downFile)
                  }  
              that.setData ({
                feliddown :true
              })
          } 
          else
          {
      wx.showToast({
        title: '请选择执勤地点',
        icon: 'success',
      })
          } 
  },
   savaExcel(userdata) {
    let that = this
    wx.cloud.callFunction({
      name: "zmyy_excel_down",
      data: {
        userdata: userdata,
        excel_name: pic_array[hx_index].value
      },
      success(res) {
        console.log("保存成功", res)
        that.getFileUrl(res.result.fileID)
      },
      fail(res) {
        console.log("保存失败", res)
      }
    })
  },
  xiaoqu_savaExcel(userdata) {
    let that = this
    wx.cloud.callFunction({
      name: "xiaoqu_jinghunanyuan",////////////传参数
      // name:"xiaoqu_jinghunanyuan",
      data: {
        userdata: userdata
      },
      success(res) {
        // console.log("保存成功", res)
        that.getFileUrl(res.result.fileID)
      },
      fail(res) {
        // console.log("保存失败", res)
      }
    })
  },
  dep_listf: function (e) {
    // console.log('马上查询数据库，findcheckbox为:')
    const db = wx.cloud.database();
    var that = this;
    db.collection('checkbox').doc('gzry_dep_list').get({
      success: function (res) {
        // console.log('马上查询数据库，:', res.data)

        that.setData({
          pic_array: res.data.gzry_dep,

        })
        pic_array = res.data.gzry_dep

      }
    })

    // console.log('马上查询数据库，findcheckbox为:', res.data.gzry_dep)
  },
  bindPickerChange_hx: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail);
    this.setData({   //给变量赋值
      hx_index: e.detail.value,  //每次选择了下拉列表的内容同时修改下标然后修改显示的内容，显示的内容和选择的内容一致
      // zqaddress: e.currentTarget.dataset.selecthx
    })
    hx_index = e.detail.value
    // zqaddress: e.currentTarget.dataset.selecthx
    console.log('自定义值:', hx_index);
    zqaddress = pic_array[e.detail.value].value
  },
 
  findOpenid: function () {
    var that = this;
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        // console.log('onGetOpenid result: ', app.globalData.openid)
        app.globalData.openid = res.result.openid

        db.collection('glry').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              this.setData({                
                downFile: res.data[0].downFile,
                gzry_name: res.data[0].name,   
                zqaddress: res.data[0].zqaddress, 
                hx_index: res.data[0].hx_index        
              })
              zqaddress = res.data[0].zqaddress,
              downFile = res.data[0].downFile,
                hx_index = res.data[0].hx_index, ///看选择的部门id
                database = res.data[0].database ///要读取的数据库,在工作人员表中的字段
              console.log('已添加过downFile', zqaddress, downFile, database)
              // name_phone = res.data[0].电话
            }            
            else {
              this.setData({
                tbry: "请到 服务中心-->工作人员绑定 后再使用扫码功能！"
              })
            }
          })
      }
    })
  },
  ///////////////////静湖南花下载
  xiaoqu_jinghunanyuan :function (e) {
    // console.log('没e有人员信息', downFile)
    let that = this;
    if (downFile=="静湖南苑") {
      wx.showToast({
        title: '上传文件请等待',
        icon: 'success',
      })
      //读取users表数据
      wx.cloud.callFunction({
        name: "xiaoqu_jinghunanyuan_getdata",///////////////传参数
        // name: "xiaoqu_jinghunanyuan_getdata",
        success(res) {
          console.log("读取成功cloud_data", res.result.data)
          that.xiaoqu_savaExcel(res.result.data)
        },
        fail(res) {
          console.log("读取失败getusers", res)
        }
      })
    }
    else {
      wx.showToast({
        title: '没有权限下载',
        icon: 'success',
      })
    }
   
  }, 

///////////////////静湖南花下载

//////////////////////////////////////
  //把数据保存到excel里，并把excel保存到云存储
 
///////////////////////////以下公用函数
  //获取云存储文件下载地址，这个地址有效期一天
  getFileUrl(fileID) {
    let that = this;
    wx.cloud.getTempFileURL({
      fileList: [fileID],
      success: res => {
        // get temp file URL
        console.log("文件下载链接", res.fileList[0].tempFileURL)
        that.setData({
          fileUrl: res.fileList[0].tempFileURL,
          feliddown:false
        })
      },
      fail: err => {
        // handle error
      }
    })
  },
  //复制excel文件下载链接
  copyFileUrl() {
    let that = this
    wx.setClipboardData({
      data: that.data.fileUrl,
      success(res) {
        wx.getClipboardData({
          success(res) {
            console.log("复制成功", res.data) // data
          }
        })
      }
    })
  }
})
